<template>
  <div v-if="retvalue == 'id'">
    <!--返回classid-->
    <el-select :style="{ width: width }" clearable filterable @change="changevt" placeholder="请选择" ref="invitemclassid"
      v-model="myvalue">
      <el-option :key="index" :label="item.itemclassname" :value="item.itemclassid" v-for="(item, index) in seloptions">
        <span style="float: left;font-weight: bold;" v-if="item.havechild == '1'">{{ item.itemclassname }}</span>
        <span style="float: left;padding-left:10px;" v-else>{{ item.itemclassname }}</span>
      </el-option>
    </el-select>
  </div>
  <div v-else>
    <!--返回classcode-->
    <el-select :style="{ width: width }" clearable filterable @change="changevt" placeholder="请选择" ref="invitemclasscode"
      v-model="myvalue">
      <el-option :key="index" :label="item.itemclassname" :value="item.itemclasscode" v-for="(item, index) in seloptions">
        <span style="float: left;font-weight: bold;" v-if="item.havechild == '1'">{{ item.itemclassname }}</span>
        <span style="float: left;padding-left:10px;" v-else>{{ item.itemclassname }}</span>
      </el-option>
    </el-select>
  </div>
</template>
<script>
export default {
  //retvalue:id返回classid,code返回classcode
  props: { width: { type: String }, value: { type: [String, Number] }, retvalue: { type: String, default: "code" } },
  watch: {
    value(newVal) {
      this.myvalue = newVal;
    },
    myvalue(newVal) {
      this.$emit("input", newVal);
    },
  },
  data() {
    return {
      myvalue: this.value,
      fulldata: null,
      seloptions: [],
    };
  },
  computed: {},
  methods: {
    changevt: function (evt) {
      let cData = this.fulldata;
      if (evt != "" && this.retvalue == "code") {
        cData = cData.filter((item) => item.itemclasscode === evt);
      } else {
        cData = cData.filter((item) => item.itemclassid === evt);
      }
      this.$emit("change", evt, cData[0]);
      this.$emit("changeObj", { code: evt, name: "", row: cData[0] });
    },
    loadAllData: function () {
      this.$tool
        .request({
          url: "ucent/ddl",
          method: "post",
          data: {
            action: "invitemclass",
          },
        })
        .then((res) => {
          this.fulldata = res;
          this.seloptions = this.fulldata;
        })
        .catch((e) => {
          console.log("erro", e);
        });
    },
  },
  mounted() {
    this.loadAllData();
  },
};
</script>
<style scoped>
.el-aside {
  color: #333;
}
</style>
